﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace PoolSide
{
    public class BlockInfo
    {
        #region Private Member Variables
        private int _Block;
        private bool _IsMature;
        private DateTime _DateFound;
        private DateTime _DateStarted;
        private decimal _TotalScore;
        private decimal _Reward;
        private int _Confirmations;
        private long _TotalShares;
        private string _Duration;
        private static bool shade = false;
        #endregion

        #region Properties
        public int Block
        {
            get { return _Block; }
            set { _Block = value; }
        }
        public bool IsMature
        {
            get { return _IsMature; }
            set { _IsMature = value; }
        }
        public DateTime DateFound
        {
            get { return _DateFound; }
            set { _DateFound = value; }
        }
        public DateTime DateStarted
        {
            get { return _DateStarted; }
            set { _DateStarted = value; }
        }
        public decimal TotalScore
        {
            get { return _TotalScore; }
            set { _TotalScore = value; }
        }
        public decimal Reward
        {
            get { return _Reward; }
            set { _Reward = value; }
        }
        public int Confirmations
        {
            get { return _Confirmations; }
            set { _Confirmations = value; }
        }
        public long TotalShares
        {
            get { return _TotalShares; }
            set { _TotalShares = value; }
        }
        public string Duration
        {
            get { return _Duration; }
            set { _Duration = value; }
        }
        #endregion

        #region Methods
        public ListViewItem GetListViewItem()
        {
            ListViewItem item = new ListViewItem();
            item.Text = _Block.ToString();
            item.SubItems.Add(_DateFound.ToString("MM/dd/yyyy HH:mm:ss"));
            item.SubItems.Add(_Duration);
            item.SubItems.Add(_TotalShares.ToString());
            item.SubItems.Add(_Reward.ToString());
            item.Tag = this;
            if (shade)
                item.BackColor = System.Drawing.Color.LightGray;
            shade = !shade;
            return item;
        }
        #endregion

        #region Constructors
        public BlockInfo()
        {
        }
        public BlockInfo(string inputJson)
        {
            string raw = "";
            int firstColon = inputJson.IndexOf(':');

            raw = inputJson.Substring(0, firstColon).Replace("'", "");
            _Block = Convert.ToInt32(raw);
            raw = inputJson.Substring(firstColon + 1);

            StringReader sr = new StringReader(raw);
            JsonTextReader tr = new JsonTextReader(sr);
            while (tr.Read())
            {
                if (tr.TokenType == JsonToken.PropertyName)
                {
                    try
                    {
                        switch (tr.Value.ToString().ToUpper())
                        {
                            case "IS_MATURE": _IsMature = tr.ReadAsString() == "1" ? true : false; break;
                            case "TOTAL_SCORE": _TotalScore = tr.ReadAsDecimal().Value; break;
                            case "MINING_DURATION": _Duration = tr.ReadAsString(); break;
                            case "DATE_FOUND":
                                // YYYY-MM-DDHH:MM:SS
                                if (DateTime.TryParseExact(
                                    tr.ReadAsString(), 
                                    "yyyy-MM-ddHH:mm:ss", 
                                    null, 
                                    System.Globalization.DateTimeStyles.None, 
                                    out _DateFound)) ;
                                break;
                            case "DATE_STARTED":
                                // YYYY-MM-DDHH:MM:SS
                                if (DateTime.TryParseExact(
                                    tr.ReadAsString(), 
                                    "yyyy-MM-ddHH:mm:ss", 
                                    null, 
                                    System.Globalization.DateTimeStyles.None, 
                                    out _DateStarted)) ;
                                break;
                            case "CONFIRMATIONS": _Confirmations = tr.ReadAsInt32().Value; break;
                            case "TOTAL_SHARES":  if (long.TryParse(tr.ReadAsString(), out _TotalShares)) ; break;
                            case "REWARD":
                                // in some cases the reward could be returned at '0E-8', so 
                                // try and parse - set to 0 if it fails.  another way to do this?
                                string val = tr.ReadAsString();
                                if (!decimal.TryParse(val, out _Reward))
                                    _Reward = 0M;
                                break;
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }
        #endregion
    }
}